package com.petecapra.cashflow.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.petecapra.cashflow.model.Account;
import com.petecapra.cashflow.model.AccountTransaction;
import com.petecapra.cashflow.model.FrequencyType;
import com.petecapra.cashflow.model.Schedule;
import com.petecapra.cashflow.model.TransactionType;
import com.petecapra.cashflow.model.dao.AccountManager;
import com.petecapra.cashflow.model.dao.AccountTransactionManager;
import com.petecapra.cashflow.model.dao.ScheduleManager;

@Controller
@RequestMapping("/accounts/{accountId}/accountTransactions*")
public class AccountTransactionsController {
	
	@Autowired private AccountManager accountManager;
	@Autowired private AccountTransactionManager accountTransactionManager;
	@Autowired private ScheduleManager scheduleManager;
	
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String create(@PathVariable("accountId") long accountId, ModelMap modelMap) {
		
		Account account = accountManager.findById(accountId);
		Schedule schedule = new Schedule(account, "", FrequencyType.None, 0, 0.0);
		
		modelMap.addAttribute("account", account);
		modelMap.addAttribute("accounts", accountManager.getAll());
		modelMap.addAttribute("schedule", schedule);
		modelMap.addAttribute("transactionTypes", TransactionType.values());
		modelMap.addAttribute("frequencyTypes", FrequencyType.values());		
		
        return "accountTransactions/create";
	}
	
	@Transactional
	@RequestMapping(value = "", method = RequestMethod.POST)
	public String create(@PathVariable("accountId") long accountId, Schedule schedule) {
		
		// save the schedule first
		schedule.save();
	
		// now update recurrences of transaction according to schedule
		schedule.updateRecurrences();
		
		return "redirect:/accounts/" + accountId;
	}
	
	@RequestMapping(value = "/{accountTransactionId}/delete", method = RequestMethod.GET)
	public String delete(@PathVariable("accountTransactionId") long accountTransactionId, ModelMap modelMap) {
		
		// find the accountTransaction
		AccountTransaction at = accountTransactionManager.findById(accountTransactionId);
		
		// and add it to the model
		modelMap.addAttribute("accountTransaction", at);
		
		return "accountTransactions/delete";
	}
	
	@Transactional
	@RequestMapping(value = "/{accountTransactionId}", method = RequestMethod.DELETE)
	public String delete(@RequestParam("delete") String delete, @PathVariable("accountId") long accountId, @PathVariable("accountTransactionId") long accountTransactionId) {
		
		AccountTransaction at = accountTransactionManager.findById(accountTransactionId);
		Schedule schedule = at.getSchedule();
		
		// if we are deleting all occurrences
		if (delete.equals("all")) {
			
			// delete the schedule
			scheduleManager.delete(schedule);
			
		// if we are deleting just this transaction
		} else {
			
			// delete the accountTransaction
			schedule.deleteTransaction(at);
			
		}
		
		return "redirect:/accounts/" + accountId;
	}
	
	@Transactional
	@RequestMapping(value = "/{accountTransactionId}/paid", method = RequestMethod.GET)
	public String paid(@PathVariable("accountId") long accountId, @PathVariable("accountTransactionId") long accountTransactionId) {
		
		// find the accountTransaction
		AccountTransaction at = accountTransactionManager.findById(accountTransactionId);
		
		// and mark it as paid
		at.markPaid();
		
		return "redirect:/accounts/" + accountId;
	}
	
}